<?php
require dirname(dirname(dirname(__FILE__))).'/unit_test_conf.php';
require dirname(__FILE__).'/Class.php';
define('NAME', array_pop(explode('/',dirname(__FILE__))));

/**
 *	@author kinuz
 *	@since 2012.07.08
 */
class UnitTest extends PHPUnit_Framework_TestCase
{
	################################################################################################################################
	#	1. Setting Basic varialbes for Testing..
	################################################################################################################################
	protected static $error;
	protected static $user_seq;
	
	################################################################################################################################
	#	2. Basic Method Testing..
	################################################################################################################################
	
	/**
	 *	사용자 정보 등록에 대한 테스트 케이스
	 *	@author kinuz
	 *	@since 2011. 12 .27
	 */
	public function testInsert4Class()
	{
		try
		{
			$o = Cl::get(NAME);
			$o['user_type']			= utRand('ADMIN','USER');
			$o['user_group_seq']	= rand(0,4);
			$o['user_id']			= utRc(rand(5,9));
			$o['user_passwd']		= utRc(rand(5,9));
			$o['user_name']			= utRc(rand(5,9));
			$o['user_nick']			= utRc(rand(5,9));
			$o['user_birth_date']	= '19'.rand(10,99).'-'.str_pad(rand(1,12), 2, '0', STR_PAD_LEFT).'-'.str_pad(rand(1,31), 2, '0', STR_PAD_LEFT);
			$o['user_phone']		= '0'.rand(1,30).'-'.rand(100,999).'-'.rand(1000,9999);
			$o['user_mobile']		= '01'.rand(1,9).'-'.rand(1000,9999).'-'.rand(1000,9999);
			$o['user_email']		= utRc(rand(5,9)).'@test.com';
			$o['user_sex']			= utRand('M','F');
			$o['user_memo']			= utRc(rand(5,9));
			$o->insert();
			self::$user_seq = $o['user_seq'];
			/************************************************************************************************************************/
			//	Compare Both Data..
			/************************************************************************************************************************/
			$c = Cl::get(NAME);
			$c->load(self::$user_seq);
			$this->assertEquals($o['user_type']			, 	$c['user_type']			, "[Error] user_type"		);
			$this->assertEquals($o['user_group_seq']	, 	$c['user_group_seq']	, "[Error] user_group_seq"	);
			$this->assertEquals($o['user_id']			,	$c['user_id']			, "[Error] user_id"			);
			$this->assertEquals($o['user_passwd']		,	$c['user_passwd']		, "[Error] user_passwd"		);
			$this->assertEquals($o['user_name']			,	$c['user_name']			, "[Error] user_name"		);
			$this->assertEquals($o['user_nick']			,	$c['user_nick']			, "[Error] user_nick"		);
			$this->assertEquals($o['user_birth_date']	,	$c['user_birth_date']	, "[Error] user_birth_date"	);
			$this->assertEquals($o['user_phone']		,	$c['user_phone']		, "[Error] user_phone"		);
			$this->assertEquals($o['user_mobile']		,	$c['user_mobile']		, "[Error] user_mobile"		);
			$this->assertEquals($o['user_email']		,	$c['user_email']		, "[Error] user_email"		);
			$this->assertEquals($o['user_sex']			,	$c['user_sex']			, "[Error] user_sex"		);
			$this->assertEquals($o['user_memo']			,	$c['user_memo']			, "[Error] user_memo"		);
			/************************************************************************************************************************/
		}
		catch(\AmException $e)
		{
			self::$error = true;
			$this->assertTrue(False, 'Exception - '.$e->getMessage());
		}
	}
	
	
	/**
	 *	사용자 정보 수정에 대한 테스트 케이스
	 *	@author kinuz
	 *	@since	2012. 12 .27
	 */
	public function testUpdate4Class()
	{
		if(self::$error == true) return;
		try
		{
			$o = Cl::get(NAME);
			$o->load(self::$user_seq);
			$o['user_type']			= utRand('ADMIN','USER');
			$o['user_group_seq']	= rand(0,4);
			$o['user_passwd']		= utRc(rand(5,9));
			$o['user_name']			= utRc(rand(5,9));
			$o['user_nick']			= utRc(rand(5,9));
			$o['user_birth_date']	= '19'.rand(10,99).'-'.str_pad(rand(1,12), 2, '0', STR_PAD_LEFT).'-'.str_pad(rand(1,31), 2, '0', STR_PAD_LEFT);
			$o['user_phone']		= '0'.rand(1,30).'-'.rand(100,999).'-'.rand(1000,9999);
			$o['user_mobile']		= '01'.rand(1,9).'-'.rand(1000,9999).'-'.rand(1000,9999);
			$o['user_email']		= utRc(rand(5,9)).'@test.com';
			$o['user_sex']			= utRand('M','F');
			$o['user_memo']			= utRc(rand(5,9));
			$o->update();
			/************************************************************************************************************************/
			//	Compare Both Data..
			/************************************************************************************************************************/
			$c = Cl::get(NAME);
			$c->load(self::$user_seq);
			$this->assertEquals($o['user_type']			, 	$c['user_type']			, "[Error] user_type"		);
			$this->assertEquals($o['user_group_seq']	, 	$c['user_group_seq']	, "[Error] user_group_seq"	);
			$this->assertEquals($o['user_id']			,	$c['user_id']			, "[Error] user_id"			);
			$this->assertEquals($o['user_passwd']		,	$c['user_passwd']		, "[Error] user_passwd"		);
			$this->assertEquals($o['user_name']			,	$c['user_name']			, "[Error] user_name"		);
			$this->assertEquals($o['user_nick']			,	$c['user_nick']			, "[Error] user_nick"		);
			$this->assertEquals($o['user_birth_date']	,	$c['user_birth_date']	, "[Error] user_birth_date"	);
			$this->assertEquals($o['user_phone']		,	$c['user_phone']		, "[Error] user_phone"		);
			$this->assertEquals($o['user_mobile']		,	$c['user_mobile']		, "[Error] user_mobile"		);
			$this->assertEquals($o['user_email']		,	$c['user_email']		, "[Error] user_email"		);
			$this->assertEquals($o['user_sex']			,	$c['user_sex']			, "[Error] user_sex"		);
			$this->assertEquals($o['user_memo']			,	$c['user_memo']			, "[Error] user_memo"		);
			/************************************************************************************************************************/
		}
		catch(\NoRecordException $e){}
		catch(\AmException $e)
		{
			self::$error = true;
			$this->assertTrue(False, 'Exception - '.$e->getMessage());
		}
	}
	
	
	/**
	 *	사용자 정보 수정에 대한 테스트 케이스
	 *	@author kinuz
	 *	@since	2012. 12. 27
	 */
	public function testLoadById4Class()
	{
		if(self::$error == true) return;
		try
		{
			$c = Cl::get(NAME);
			$c->load(self::$user_seq);
			$c->loadById($c['user_id']);
		}
		catch(\NoRecordException $e){}
		catch(\AmException $e)
		{
			self::$error = true;
			$this->assertTrue(False, 'Exception - '.$e->getMessage());
		}
	}
	
	################################################################################################################################
	#	3. Manager Method Testing..
	################################################################################################################################
	/**
	 *	@author kinuz
	 *	@since	2012. 01. 29
	*/
	public function testLoad4Manager()
	{
		if(self::$error == true) return;
		try
		{
			$cm = Cl::get(NAME.'Manager');
			$cm->load();
		}
		catch(\NoRecordException $e){}
		catch(\AmException $e)
		{
			self::$error = true;
			$this->assertTrue(False, 'Exception - '.$e->getMessage());
		}
	}
	
	/**
	 *	@author kinuz
	 *	@since	2012. 02. 01
	*/
	public function testLoadAll4Manager()
	{
		if(self::$error == true) return;
		try
		{
			$cm = Cl::get(NAME.'Manager');
			$cm->loadAll();
		}
		catch(\NoRecordException $e){}
		catch(\AmException $e)
		{
			self::$error = true;
			$this->assertTrue(False, 'Exception - '.$e->getMessage());
		}
	}	
	
	################################################################################################################################
	#	4. Scenario Testing..
	#	-> a)
	################################################################################################################################
	
	################################################################################################################################
	#	5. Testing tool Methods
	################################################################################################################################
	private function removeTestData()
	{
		$ci = get_instance();
		$ci->db->query("DELETE FROM `user` WHERE `user_seq` = ".self::$user_seq);
	}
	public function setUp()
	{
		//	no need..
	}
	public function tearDown()
	{
		//	no need..
	}
	################################################################################################################################
}
?>